<?php
function old_encrypt($str)
{
	import('ORG.Crypt.Crypt');
    $key = C('DES_KEY');
    $des = new Crypt ();
    return $des->encrypt($str,$key,1);
}

function old_decrypt($str)
{
	import('ORG.Crypt.Crypt');
    $key = C('DES_KEY');
    $des = new Crypt ();
    return $des->decrypt($str,$key,1);
}

function decrypt ($decrypt){
	$key = C('DES_KEY');
    
    // 不需要設定 IV
    $str = mcrypt_decrypt(MCRYPT_DES, $key, base64_decode($decrypt), MCRYPT_MODE_ECB);

    // 根據 PKCS#7 RFC 5652 Cryptographic Message Syntax (CMS) 修正 Message 移除 Padding
    $pad = ord($str[strlen($str) - 1]);
    return substr($str, 0, strlen($str) - $pad);
}

function encrypt ($encrypt){
	$key = C('DES_KEY');
    
    // 根據 PKCS#7 RFC 5652 Cryptographic Message Syntax (CMS) 修正 Message 加入 Padding
    $block = mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_ECB);
    $pad = $block - (strlen($encrypt) % $block);
    $encrypt .= str_repeat(chr($pad), $pad);

    // 不需要設定 IV 進行加密
    $passcrypt = mcrypt_encrypt(MCRYPT_DES, $key, $encrypt, MCRYPT_MODE_ECB);
    return base64_encode($passcrypt);
}


function getclass($id)
{
	$class = M('class');
	$info = $class ->find($id);
	$arr['id'] = $info['id'];
	$arr['name'] = $info['class_name'];
	return $arr;
	unset($class);
}
function getschool($id)
{
	
	$school = M('school');
	$info = $school ->find($id);
	$arr['id'] = $info['id'];
	$arr['name'] = $info['XXMC'];
	return $arr;
	unset($school);

}
function loadinfo($id,$type)
{
	$map['user_id']= $id;
	if ($type == 1)
    {
    	$mteacher = M('teacher');
        $info = $mteacher ->where($map)->find();
    }
    else if ($type == 2)
    {
    	$mparent = M('parent');
        $info = $mparent ->where($map)->find();
    }
    return $info;
    unset($mteacher);
    unset($mparent);
	unset($info);
}
function loadm($id,$type)
{
	$muser =M('user');
    $userinfo = $muser->find($id);
    if ($type == 1)
    {
    	return $userinfo['is_teacher'];
    }
    else if ($type == 2)
    {
    	return $userinfo['is_parent'];
    }
    unset($muser);
	unset($userinfo);

}

function loadpage($pagesize)
{
	$page = $_REQUEST['pageNO'];
	$page_size = $pagesize;
	if($page && $page_size)
	{
		$return[0] = ($page -1)*$page_size;
		$return[1] = $page_size;
		$return[2] = $page;
		return $return;
	}
	else
	{
		$action =$_REQUEST['action'];
        $arr[$action]['state'] = 'error';
        $arr[$action]['code'] = 307;
        echo json_encode($arr);
        exit;
	}
}
function loadphone($id,$type)
{
	$muser =M('user');
    $userinfo = $muser->find($id);
    if ($type == 1)
    {
    	return $userinfo['SJHM'];
    }
    else if ($type == 2)
    {
    	return $userinfo['SJHM'];
    }
    unset($muser);
	unset($userinfo);
}
function getproclamation($sc_or_cl,$mapid,$page)
{
	if ($sc_or_cl==1)
	{
		$scproclamation = M('school_proclamation');
		$scprolist = $scproclamation ->join(' proclamation_type ON school_proclamation.proclamation_type_id = proclamation_type.id') ->join('teacher ON teacher.id = school_proclamation.teacher_id') ->field('school_proclamation.id as proclamation_id,proclamation_type.id as proclamation_type,title as proclamation_title,content as proclamation_content,FBSJ as proclamation_time,teacher.XM as proclamation_promulgator')->where('school_proclamation.school_id ='.$mapid)->limit($page[0],$page[1])->order('school_proclamation.id desc')->select();


		$scproclamationcount = $scproclamation ->where('school_proclamation.school_id ='.$mapid) ->count();
		$nowcount = count($scprolist)+$page[2]*$page[1];
		if ($scproclamationcount>$nowcount)
		{
			$isNext =1;
		}
		else
		{
			$isNext =0;
		}

		for($i=0;$i<count($scprolist);$i++)
            {
                $scprolist[$i]['proclamation_url'] =C('PATH').__URL__.'/view/type/schproclamation/id/'.$scprolist[$i]['proclamation_id'];

            }

         $returnlist['list'] = $scprolist;
         $returnlist['isNext'] = $isNext;
		 return $returnlist;//学校公告
	}
	else if ($sc_or_cl==2)
	{
		$clproclamation = M('class_proclamation');
		$mproclamation_log =M('proclamation_log');
		$mstudent = M('student');
        $clprolist = $clproclamation ->join(' proclamation_type ON class_proclamation.proclamation_type_id = proclamation_type.id,class_id') ->join('teacher ON teacher.id = class_proclamation.teacher_id') ->field('proclamation_type.id as proclamation_type,class_proclamation.id as proclamation_id,title as proclamation_title,content as proclamation_content,FBSJ as proclamation_time,teacher.XM as proclamation_promulgator,class_proclamation.class_id')->where('class_proclamation.class_id in ('.$mapid.')')->order('class_proclamation.id desc')->limit($page[0],$page[1])->select();
        //echo $clproclamation->getlastsql();
        	for($i=0;$i<count($clprolist);$i++)
            {
                $clprolist[$i]['proclamation_url'] =C('PATH').__URL__.'/view/type/clsproclamation/id/'.$clprolist[$i]['proclamation_id'];
                

                $studentlist =$mstudent ->field('student.XM as student_name,student.parent_id') ->where('student.class_id='.$clprolist[$i]['class_id'])->join("parent on parent.id =student.parent_id")->select();

                for($j=0;$j<count($studentlist);$j++)
            	{
            		$plc= $mproclamation_log ->where('parent_id='.$studentlist[$j]['parent_id'])->count();
            		if ($plc>0)
            		{
            			$studentlist[$j]['isAffirm'] = 1;
            		}
            		else
            		{
            			$studentlist[$j]['isAffirm'] = 0;
            		}
            	}
                $clprolist[$i]['prarent_list'] =$studentlist;
                //$clprolist[$i]['parent_list'] = 

            }

		

		$clproclamationcount = $clproclamation ->where('class_proclamation.class_id in ('.$mapid.')') ->count();
		$nowcount = count($clprolist)+$page[2]*$page[1];
		if ($clproclamationcount>$nowcount)
		{
			$isNext =1;
		}
		else
		{
			$isNext =0;
		}
 		$returnlist['list'] = $clprolist;
        $returnlist['isNext'] = $isNext;
		return $returnlist;
        //return $clprolist;//学校公告
	}
	unset($scproclamation);
	unset($scprolist);
	unset($clproclamation);
	unset($clprolist);

}

function loadclsles($id,$day,$lesson,$t='')
{
	$mlesson=M('lesson');
	$lessoninfo = $mlesson->field('subject_id,teacher_id')->where('schedule.XXZT=1 and week_day='.$day.' and lesson_type_id ='.$lesson.' and schedule.class_id ='.$id)->join('schedule on schedule.id =lesson.schedule_id')->find();
	if (empty($lessoninfo))
	{
		return "";
	}
	else
	{
		$subject= M('subject');
		$subjectinfo = $subject ->find($lessoninfo['subject_id']);
		//$teacherinfo = loadinfo($lessoninfo['teacher_id'],1);
		if ($t==1)
		{
			return $subjectinfo['KCMC'];
		}
		else
		{
			return $subjectinfo['KCMC'].'<br>'.getinfo($lessoninfo['teacher_id'],'teacher','XM');
		}
	}

}

function loadsubinfo($exam_id,$subject_id,$class_id)
{
	$map['subject_id']=$subject_id;
	$map['exam_id'] =$exam_id;
	$map['_string'] =' (student_id in (select id from student where class_id ='.$class_id.'))  ';
	$mresult = M('student_result');
	$count = $mresult->where($map)->count();
	return $count;


}
function replacestr($str)
{
	return str_replace("\n", '<br/>',$str);
}
function loadattence($num,$time,$stuid,$class_id,$week)
{

	$mstudent_attence = M('student_attence');
	 $info = $mstudent_attence->where('student_attence.student_id ='.$stuid.' and schedule.XXZT=1 and student_attence.FBSJ='.$time.' and lesson.lesson_type_id='.$num.' and week_day='.$week)->join("lesson on lesson.id =student_attence.lesson_id")->join('schedule on schedule.id =lesson.schedule_id')->find();
	if (!empty($info))
	{
		if ($info['status']==1)
		{
			return '<span class="status green">已到</span>';
		}
		else if($info['status']==0)
		{
			return '<span class="status red">未到</span>';
		}

		//return "<span class='blue'>正常</span>";
	}
	else
	{
		return "";
	}
	
}
function loadlesson($id,$day,$lesson)
{
	$mlesson=M('lesson');
	$lessoninfo = $mlesson->field('subject_id')->where('schedule.XXZT=1 and week_day='.$day.' and lesson_type_id ='.$lesson.' and lesson.teacher_id ='.$id)->join('schedule on schedule.id =lesson.schedule_id')->find();
	if (empty($lessoninfo))
	{
		return "";
	}
	else
	{
		$subject= M('subject');
		$subjectinfo = $subject ->find($lessoninfo['subject_id']);
		//$teacherinfo = loadinfo($id,1);
		return $subjectinfo['KCMC'].'<br>'.getinfo($id,'teacher','XM');
	}
}
function getinfo($id,$table,$field)
{
	$parent = M($table);
	if (empty($id))
	{
		return '';
	}
	$info = $parent ->find($id);
	return $info[$field];
	unset($parent);
}

function loadexresult($id)
{
	$f=$_REQUEST['f'];
	$map['student_id']=$id;
	$mstudent_result =M('student_result');
	$resultlist = $mstudent_result->where($map)->select();
	foreach ($resultlist as $rlt) {
		$examidlist .= $rlt['exam_id'].',';
	}
	$examidlist =rtrim($examidlist,',');
	if (!empty($_REQUEST['name']))
	{
		$wstr =" and exam_name like '%".$_REQUEST['name']."%'";
	}
	if (!empty($_REQUEST['exam_id']))
	{
		$wstr .= " and id =".$_REQUEST['exam_id'];
	}
	$mexam=M('exam');

	$examlist = $mexam->where('id in ('.$examidlist.')'.$wstr)->select();
	foreach ($examlist as $exl) {
		$sum = '';
		$sum = $mstudent_result->where('exam_id='.$exl['id'].' and student_id='.$id)->sum('FSLKSCJ');
		//$str .= '<tr style="background:url(__TMPL__images/sbb.gif)" ><td><b>'.getinfo($exl['id'],'exam','exam_name').'</b><br /><span>'.getinfo($id,'student','XM').'</span></td><td style="background:url(__TMPL__images/sb.gif) no-repeat ;width:205px; height:110px;color:red;font-weight:bold;"><a href="__URL__/viewresult/stid/'.$id.'/exid/'.$exl['id'].'"  style="width:205px; height:110px;color:red;font-weight:bold;text-decoration:none;display:block;line-height:130px;">&nbsp;'.$sum.'分</a></td></tr>';
		$str ='<li><a href="__URL__/viewresult/stid/'.$id.'/exid/'.$exl['id'].'/f/'.$f.'" title="'.getinfo($exl['id'],'exam','exam_name').'">
					<div class="score">
						'.$sum.'分
					</div>
					<div class="">
						<h3>'.getinfo($exl['id'],'exam','exam_name').'</h3>
						<p>'.getinfo($id,'student','XM').'</p>
					</div>
					<span class="more"></span>
					</a>
				</li>';
	}
	return $str;
	 
}
function loadscoreresult($exid,$student_id,$subject_id)
	{
		$mstudent_result= M('student_result');
		$map['exam_id']=$exid;
		$map['student_id']=$student_id;
		$map['subject_id']=$subject_id;
		$result = $mstudent_result->where($map)->find();
		echo  $result['FSLKSCJ'];
	}

function loadscore($exam_id,$student_id,$t)
	{
		$mstudent_result =M('student_result');
		$map['exam_id'] =$exam_id;
		$map['student_id'] =$student_id;


		if ($t==1)
		{
			$resultlist =$mstudent_result->where($map)->select();
			foreach ($resultlist as $vl) {
				$result .= getinfo($vl['subject_id'],'subject','KCMC').'：'.$vl['FSLKSCJ'].'<br />';
				# code...
			}
		}
		if ($t==2)
		{
			$result =$mstudent_result->where($map)->sum('FSLKSCJ');
		}
		if ($t==3)
		{
			$total = $mstudent_result->where($map)->sum('FSLKSCJ');
			$num = 	$mstudent_result->where($map)->count();
			$result =$total/$num;
		}
		if ($t==5)
		{
			$mexam =M('exam');
			$list = $mstudent_result->field('student_id')->group('student_id')->select();
			$i=1;
			foreach ($list as $ls) {

				$arr[$i]['total'] = $mstudent_result->where('exam_id='.$exam_id.' and student_id='.$ls['student_id'])->sum('FSLKSCJ');
				$arr[$i]['student_id'] = $ls['student_id'];
				$i++;
			}
			$j=1;
			$arr = array_sort($arr,'total','desc');

			foreach ($arr as $lt) {

				if ($lt['student_id'] == $student_id)
				{
					$result = $j;
					break;
				}
				$j++;
			}
		}
		if ($t==4)
		{
			$mexam =M('exam');
			$class_id =getinfo($student_id,'student','class_id');
			//$m=M('');

			//$stlist = $mstudent_result->field('student_id')->group('student_id')->select();
			$sql = 'select id as student_id from student where id in (select student_id from student_result where exam_id ='.$exam_id.' ) and class_id ='.$class_id;
			$mq =M('');
			$list =$mq->query($sql);
			$i=1;
			foreach ($list as $ls) {

				$arr[$i]['total'] = $mstudent_result->where('exam_id='.$exam_id.' and student_id='.$ls['student_id'])->sum('FSLKSCJ');
				$arr[$i]['student_id'] = $ls['student_id'];
				$i++;
			}
			$j=1;
			$arr = array_sort($arr,'total','desc');

			foreach ($arr as $lt) {

				if ($lt['student_id'] == $student_id)
				{
					$result = $j;
					break;
				}
				$j++;
			}
		}
		echo $result;
	}
	function array_sort($arr,$keys,$type='asc'){
	$keysvalue = $new_array = array();
	foreach ($arr as $k=>$v){
		$keysvalue[$k] = $v[$keys];
	}
	if($type == 'asc'){
		asort($keysvalue);
	}else{
		arsort($keysvalue);
	}
	reset($keysvalue);
	foreach ($keysvalue as $k=>$v){
		$new_array[$k] = $arr[$k];
	}
	return $new_array;
	}
function loadpower($type,$id)
	{
		$mclass=M('class');
		$mteacher_power =M('teacher_power');
		$map['teacher_id']=$id;
		
		if ($type==1)
		{
			$classlist = $mclass->where($map)->select();
			foreach ($classlist as $cl) {
				$idlist .= $cl['id'].',';
			}

		}
		else if ($type==2)
		{
			$powerlist = $mteacher_power->where($map)->select();
			foreach ($powerlist as $pl) {
				$idlist .= $pl['class_id'].',';
			}
		}
		else if ($type==3)
		{
			$classlist = $mclass->where($map)->select();
			foreach ($classlist as $cl) {
				$idlist .= $cl['id'].',';
			}
			$powerlist = $mteacher_power->where($map)->select();
			foreach ($powerlist as $pl) {
				$idlist .= $pl['class_id'].',';
			}
		}

		$idlist .= '0';
		return  $idlist;

	}

	function loadalllesson($id)
	{
		$url ='http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].__URL__.'/lesson_parent/student_id/'.$id;
		$content = file_get_contents($url);
		return $content;
	}

?>